<template>
  <div class="course-file">
    <div v-for="item in fileList" :key="item.id" class="course-file__item">
      <div class="picture">
        <img src="@assets/images/course/courseware_img.png" alt="" />
      </div>
      <div class="content">
        <div class="title">{{ item.attach_title }}</div>
        <div class="desc">
          <span
            >下载<b>{{ item.download_count || 0 }}</b
            >次</span
          >
          <span class="line"></span>
          <span>大小：{{ item.attach_data.filesize }}</span>
        </div>
      </div>
      <div class="tool">
        <el-button
          v-if="course_buy == 1"
          class="download"
          icon="edu-icon-xiazai"
          size="mini"
          plain
          round
          v-down="item.attach_data.fileurl"
          @click="handleDownload(item)"
          >下载</el-button
        >
      </div>
    </div>

    <edu-no-data v-if="fileList.length == 0" :image="false" text="暂时还没有课件哦~"></edu-no-data>
  </div>
</template>

<script>
// eslint-disable-next-line no-undef
Vue.directive('down', {
  inserted: (el, binding) => {
    el.style.cssText = 'cursor: pointer;color:write;'
    el.addEventListener('click', () => {
      let link = document.createElement('a')
      let url = binding.value
      fetch(url)
        .then(res => res.blob())
        .then(blob => {
          // 将链接地址字符内容转变成blob地址
          link.href = URL.createObjectURL(blob)
          link.download = ''
          document.body.appendChild(link)
          link.click()
        })
    })
  }
})
import EduNoData from '@/components/EduNoData'
export default {
  components: {
    EduNoData
  },
  props: {
    // 课程是否购买
    course_buy: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      fileList: []
    }
  },
  methods: {
    getFileData() {
      this.$api.getCourseCourseware(this.$route.params.id, res => {
        const { data } = res
        this.fileList = data
      })
    },
    handleDownload(item) {
      item.download_count++
      this.$api.postCourseCoursewareRecord({
        course_id: this.$route.params.id,
        ware_id: item.attach_id
      })
    }
  },
  mounted() {
    this.getFileData()
  }
}
</script>

<style lang="scss" scoped>
.course-file {
  padding-left: 25px;
  padding-right: 25px;
  &__item {
    display: flex;
    align-items: center;
    padding: 20px 0;
    border-bottom: 1px solid #ebeef5;
    .picture {
      width: 44px;
      height: 44px;
      img {
        display: block;
        width: 100%;
        height: 100%;
      }
    }
    .content {
      flex: 1;
      min-width: 0;
      margin: 0 12px;
      .title {
        font-size: 14px;
        font-weight: 400;
        color: $text-color-import;
        line-height: 16px;
        @include ellipsis();
      }
      .desc {
        margin-top: 10px;
        font-size: 12px;
        font-weight: 400;
        color: $text-color-auxiliary;
        line-height: 18px;
        display: flex;
        align-items: center;
        .line {
          margin: 0 8px;
          width: 1px;
          height: 10px;
          background-color: #e4e7ed;
        }
        b {
          color: $text-color-import;
          font-weight: normal;
        }
      }
    }
    .tool {
      .download {
        @include edu-color-primary(border-color);
        @include edu-color-primary(color);
      }
      .download:focus,
      .download:hover {
        color: #fff !important;
        @include edu-color-primary(background-color);
      }
    }
  }
}
</style>
